// ---------- SA:MP LVDM.pwn Squirrel Script -----------
// ----------- Converted for LU by: VRocker ------------

// NOTE: Anything marked with !*! is the pawn alternative

CashScoreOld <- 0;

COLOR_YELLOW <- Colour( 255, 255, 0 );
COLOR_WHITE <- Colour( 255, 255, 255 );

const PocketMoney = 15000; // Amount player recieves on spawn.

const SCRIPT_LOCATION = "Scripts/lcdm/";

iSpawnSet <- array( GetMaxPlayers() );

// unused, but shows how its declared
// local gLastGaveCash <- array( MAX_PLAYERS );

function onServerStart()
{
	// This function is pretty much the same as main() in SA:MP. but its not required
	print("----------------------------------");
	print("  Running LCDM ~MoneyGrub");
	print("         Coded By");
	print("            VRocker");
	print("----------------------------------");
}

// !*! OnGameModeInit
function onScriptLoad()
{
	LoadModule( "lu_hashing" ); //Needed for registering/password hashing
	
	dofile( SCRIPT_LOCATION + "Functions.nut" );
	LoadScripts();
	LoadHashes();
	
	dofile( SCRIPT_LOCATION + "AdminCommands.nut" );
	InitAdminCommandSystem();
	
	SetGamemodeName( "Liberty's DM~MG" );
	
	// We can add classes and vehicles here, but for now we'll leave them in content.xml
	
	NewTimer( "MoneyGrubScoreUpdate", 1000, 1 );
	
}

function MoneyGrubScoreUpdate()
{
	local CashScore = 0;
	for ( local i = 0; i < MAX_PLAYERS; i++ )
	{
		// !*! if (IsPlayerConnected(i))
		local plr = FindPlayer( i );
		if ( plr )
		{
			CashScore = plr.Cash;
			plr.Score = CashScore;
			if ( CashScore > CashScoreOld )
			{
				CashScoreOld <- CashScore;
				//Message( "$$$ " + plr.Name + " is now in the lead $$$", COLOR_YELLOW );
			}
		}
	}
}

// !*! public OnPlayerConnect(playerid)
function onPlayerJoin( plr )
{
	// !*! GameTextForPlayer(playerid,"~w~SA-MP: ~r~Las Venturas ~g~MoneyGrub",5000,5);
	BigMessage( plr, "~w~Alphazors ~r~Aussie ~g~Server", 5000, 5 );
	
	// !*! SendPlayerFormattedText(playerid, "Welcome to Las Venturas MoneyGrub, For help type /help.", 0);
	MessagePlayer( "Welcome to Alphazors Aussie Server, For help type /help.", plr );

	// Unused in lvdm
	// gLastGaveCash[ plr.ID ] = GetTickCount();
}

// !*! public OnPlayerCommandText(playerid, cmdtext[])
function onPlayerCommand( plr, cmd, text )
{

	local cmd = cmd.tolower(), level = GetPlayerLevel( plr );
	
	
	if ( cmd == "help" )
	{
		MessagePlayer("Welcome to Alphazors Aussie Server.", plr);
		MessagePlayer("Type: /objective : to find out what to do in this gamemode.", plr);
		MessagePlayer("Type: /givecash [playerid] [money-amount] to send money to other players.", plr);
		MessagePlayer("Type: /tips : to see some tips from the creator of the gamemode.", plr);
		MessagePlayer("Type: /rules : to see the server rules.", plr);
        MessagePlayer("Type: /owners : to see the owners of the server.", plr);
        MessagePlayer("Type: /forum : to see our forum link.", plr);
        MessagePlayer("Type: /credits: to see who all helped to make this gamemode/server.", plr);
	
	}
	else if ( cmd == "objective" )
	{
		MessagePlayer("This is a simple gamemode, there's no specific end in this server.", plr);
		MessagePlayer("In this gamemode, when you kill a player, you will receive whatever money they have.", plr);
		MessagePlayer("Consequently, if you have lots of money, and you die, your killer gets your cash.", plr);
		// No casinos in Liberty City, so no message saying to play them!
	}
	else if ( cmd == "tips" )
	{
		MessagePlayer("Spawning with just 1 gun might sound lame, however the idea of this", plr);
		MessagePlayer("gamemode is to get some cash, get better guns, then go after whoever has the", plr);
		MessagePlayer("most cash. Once you've got the most cash, the idea is to stay alive(with the", plr);
		MessagePlayer("cash)until the game ends, simple right? :).", plr);
	}
	else if ( cmd == "rules")
	{       
		MessagePlayer("The server rules:", plr);
		MessagePlayer("1.No insulting by any kind", plr);
		MessagePlayer("2.Respect every player like you want that they respect you", plr);
		MessagePlayer("3.Respect every admins decision", plr);
		MessagePlayer("4.Dont spam", plr);
		MessagePlayer("5.Have fun :)", plr);
	}
	else if ( cmd == "givecash" )
	{
		local tmp = GetTok( text, " ", 1 );
		print( tmp );
		// !*! if(!strlen(tmp)) {
		if ( !tmp )
		{
			MessagePlayer( "USAGE: /givecash [playerid] [amount]", plr, COLOR_WHITE );
			return 1;
		}
		local giveplayerid = tmp.tointeger();
		
		tmp = GetTok( text, " ", 2 );
		if ( !tmp )
		{
			MessagePlayer( "USAGE: /givecash [playerid] [amount]", plr, COLOR_WHITE );
			return 1;
		}
		
		local moneys = tmp.tointeger();
		
		local giveplr = FindPlayer( giveplayerid );
		if ( giveplr )
		{
			local playermoney = plr.Cash;
			if ( moneys > 0 && playermoney >= moneys )
			{
				plr.Cash -= moneys;
				giveplr.Cash += moneys;
				
				MessagePlayer( "You have sent " + givePlr.Name + "(player: " + givePlr.ID + "), $" + moneys + ".", plr, COLOR_YELLOW );
				MessagePlayer( "You have recieved $" + moneys + " from " + plr.Name + "(player: " + plr.ID + ").", givePlr, COLOR_YELLOW );
			}
			else
				MessagePlayer( "Invalid transaction amount.", plr, COLOR_YELLOW );
		}
		else
		{
			MessagePlayer( giveplayerid + " is not an active player.", plr, COLOR_YELLOW );
		}
	}
	else if ( cmd == "owners" )
            {        
                    MessagePlayer("The Owners of this server are: Alphazor.", plr);
        }
        else if ( cmd == "forum" )
        {
                MessagePlayer("The forum url is:www.comingsoon.com", plr);
        }
        else if ( cmd == "credits" )
        {    
                MessagePlayer("VRocker for making the base of the script.", plr);
                MessagePlayer("Alphazor for editing the gamemode.", plr);        
        }
		
	else if ( ProcessAccountCommands( plr, level, cmd, text ) )
		return 1;
	
	else if ( ProcessAdminCommands( plr, level, cmd, text ) )
		return 1;
}

function onPlayerSpawn( plr, spawn )
{
	plr.Cash += PocketMoney;
	
	SetPlayerRandomSpawn( plr );
}

function SetPlayerRandomSpawn( plr )
{
	if ( iSpawnSet[ plr.ID ] == 1 )
	{
	
	}
	else if ( iSpawnSet[ plr.ID ] == 0 )
	{
	
	}
}


// Liberty Unleashed has public OnPlayerDeath(playerid, killerid, reason) split into 2 parts.
// onPlayerKill is called when somebody kills a player, onPlayerDeath is called when the player commits suicide
function onPlayerKill( killer, plr, weapon, bodypart )
{
	plr.Score += 1;
	local playercash = plr.Cash;
	if ( playercash > 0 )
	{
		killer.Cash += playercash;
		plr.Cash = 0;
	}
}

function onPlayerDeath( plr, reason )
{
plr.Cash = 0;
}

function onPlayerJoin( plr )
{
	// Process the join in the accounts system
	ProcessAccountPlayerJoin( plr );
}

function onPlayerPart( plr, reason )
{
	// Process the part in the accounts system
	ProcessAccountPlayerPart( plr );
	
	// Save the data
	SaveHashes();
}
